package com.platform.web.controller.report;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.platform.base.domain.*;
import com.platform.base.service.*;
import com.platform.common.core.controller.BaseController;
import com.platform.common.core.domain.AjaxResult;
import com.platform.common.utils.redis.RedisUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 设备统计报表控制类
 *
 * @author wangqi
 * @date 2020-08-04
 */
@Controller
@RequestMapping("/report/equipment")
public class EquipmentReportController extends BaseController {

    private String prefix = "report/equipment";

    @Autowired
    private IValveTypeService valveTypeService;

    @Autowired
    private IValveInfoService valveInfoService;

    @Autowired
    private IMeterTypeService meterTypeService;

    @Autowired
    private IMeterInfoService meterInfoService;

    @Autowired
    private ICollectorTypeService collectorTypeService;

    @Autowired
    private ICollectorInfoService collectorInfoService;

    @Autowired
    private RedisUtil redisUtil;

    @RequiresPermissions("report:equipment:view")
    @GetMapping()
    public String equipment()
    {
        return prefix + "/equipment";
    }

    /**
     * 阀门类型统计
     */
    @PostMapping("/valveTypeSummary")
    @ResponseBody
    public AjaxResult valveTypeSummary()
    {
        List<ValveType> valveTypeList  = valveTypeService.list();
        for(ValveType valveType : valveTypeList){
            int count = valveInfoService.count(new QueryWrapper<ValveInfo>().eq("type_id", valveType.getTypeId()));
            valveType.setCount(count);
        }
        return AjaxResult.success(valveTypeList);
    }

    /**
     * 阀门数据统计
     */
    @PostMapping("/valveInfoSummary")
    @ResponseBody
    public AjaxResult valveInfoSummary()
    {
        List<ValveInfo> valveInfoList = new ArrayList<>();

        ValveInfo info = new ValveInfo();
        info.setTypeName("未登记阀门");
        info.setCount(valveInfoService.count(new QueryWrapper<ValveInfo>().eq("register_flag", "0")));
        valveInfoList.add(info);

        info = new ValveInfo();
        info.setTypeName("已登记阀门");
        info.setCount(valveInfoService.count(new QueryWrapper<ValveInfo>().eq("register_flag", "1")));
        valveInfoList.add(info);

        return AjaxResult.success(valveInfoList);
    }

    /**
     * 热表类型统计
     */
    @PostMapping("/meterTypeSummary")
    @ResponseBody
    public AjaxResult meterTypeSummary()
    {
        List<MeterType> meterTypeList  = meterTypeService.list();
        for(MeterType meterType : meterTypeList){
            int count = meterInfoService.count(new QueryWrapper<MeterInfo>().eq("type_id", meterType.getTypeId()));
            meterType.setCount(count);
        }
        return AjaxResult.success(meterTypeList);
    }

    /**
     * 热表数据统计
     */
    @PostMapping("/meterInfoSummary")
    @ResponseBody
    public AjaxResult meterInfoSummary()
    {
        List<MeterInfo> meterInfoList = new ArrayList<MeterInfo>();

        MeterInfo info = new MeterInfo();
        info.setTypeName("未登记热表");
        info.setCount(meterInfoService.count(new QueryWrapper<MeterInfo>().eq("register_flag", "0")));
        meterInfoList.add(info);

        info = new MeterInfo();
        info.setTypeName("已登记热表");
        info.setCount(meterInfoService.count(new QueryWrapper<MeterInfo>().eq("register_flag", "1")));
        meterInfoList.add(info);

        return AjaxResult.success(meterInfoList);
    }

    /**
     * 采集器类型统计
     */
    @PostMapping("/collectorTypeSummary")
    @ResponseBody
    public AjaxResult collectorTypeSummary()
    {
        List<CollectorType> collectorTypeList  = collectorTypeService.list();
        for(CollectorType collectorType : collectorTypeList){
            int count = collectorInfoService.count(new QueryWrapper<CollectorInfo>().eq("type_id", collectorType.getTypeId()));
            collectorType.setCount(count);
        }
        return AjaxResult.success(collectorTypeList);
    }

    /**
     * 采集器数量统计
     */
    @PostMapping("/collectorInfoSummary")
    @ResponseBody
    public AjaxResult collectorInfoSummary()
    {
        int allocated = 0, unallocated = 0;

        List<CollectorInfo> list = collectorInfoService.list();
        for(CollectorInfo collectorInfo : list){
            if(redisUtil.hHasKey("sys-netty", collectorInfo.getCollectorId())){
                allocated++;
            }else{
                unallocated++;
            }
        }

        List<CollectorInfo> collectorInfoList = new ArrayList<CollectorInfo>();

        CollectorInfo info = new CollectorInfo();
        info.setTypeName("在线采集器");
        info.setCount(allocated);
        collectorInfoList.add(info);

        info = new CollectorInfo();
        info.setTypeName("离线采集器");
        info.setCount(unallocated);
        collectorInfoList.add(info);

        return AjaxResult.success(collectorInfoList);
    }
}
